// subcontract/purchase/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    flag: false,
    bannerList: ['/static/ii.jpg', '/static/ii.jpg', '/static/ii.jpg'],//轮播图
    activityList: [],//
    page: 1,//页码
    pagingState: false,//分页状态
    LevelOneList: [],//分类列表
    activeIndex: 0,
    firstItem: {},
    current: Date.now(),
  },
  /**提交预约 */
  async addJcaiJoin(event) {
    console.log(event);
    let  {id,index} = event.currentTarget.dataset
    let object = {
      user_id: getApp().userinfo.id,
      jcai_id:id
    }
    const { code, msg } = await getApp().api.addJcaiJoin(object)
    if (code == 1) {
      getApp().showToast(msg)
      
      this.data.activityList[index].yu_status = 1
      console.log(this.data.activityList);
      this.setData({
        activityList:this.data.activityList,
      })
    }
  },
  /**返回 */
  navigateBack() {
    wx.navigateBack()
  },
  /** 跳转 */
  navigateTo(event) {
    let url = event.currentTarget.dataset.url
    wx.navigateTo({
      url,
    })
  },
  /**分类列表 */
  async getLevelOneList() {
    let object = {
      is_have: 1
    }
    const { code, data } = await getApp().api.getLevelOneList(object)
    if (code == 1) {
      // this.setData({
      //   LevelOneList: [{ id: 0, name: "众筹热榜" }, { id: 81, name: "众筹预约" }, ...data]
      // })
    }
    this.setData({
      LevelOneList: [{ id: 0, name: "众筹热榜" }, { id: 81, name: "众筹预约" }]
    })
    this.activity()
  },
  /**众筹列表 */
  async activity() {
    let index = this.data.activeIndex
    console.log();
    let object
    if (index == 0 || index == 1) {
      object = {
        page_no: this.data.page,
        first_cate_id: '',
        is_start: index,
        page_size:3
      }
    } else {
      object = {
        page_no: this.data.page,
        first_cate_id: this.data.LevelOneList[index].id,
        is_start: ''
      }
    }
    console.log(object);
    if (!this.data.pagingState) {
      const { code, data } = await getApp().api.activity(object)
      if (code == 1) {
        this.setData({
          firstItem: data.first_item,
          activityList: this.data.activityList.concat(data.list)
        })
        if (data.more != 1) {
          this.setData({
            pagingState: true
          })
          return
        }
        this.setData({
          page: (this.data.page * 1) + 1
        })
      }
    }
  },
  /**分类回调 */
  onChange(event) {
    this.setData({
      activeIndex: event.detail.index
    })
    this.setData({
      page: 1,//页码
      pagingState: false,//分页状态
      activityList: [],//
    })
    this.activity()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

    this.getLevelOneList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.activity()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  onPageScroll(event) {
    this.setData({
      flag: event.scrollTop >= 200 ? true : false
    })
  },
})